package me.firstapp.service.weixin.account.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.utils.Page;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.weibi.WeiBiAccount;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace.ChangeType;
import me.firstapp.repository.weixin.member.MemberRepository;
import me.firstapp.repository.weixin.weibi.WeiBiAccountRepository;
import me.firstapp.repository.weixin.weibi.WeiBiAccountTraceRepository;
import me.firstapp.service.weixin.account.WeiBiAccountTraceService;

@Service
public class WeiBiAccountTraceServiceImpl implements WeiBiAccountTraceService {

	@Autowired
	private WeiBiAccountTraceRepository weiBiAccountTraceRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private WeiBiAccountRepository weiBiAccountRepository;

	@Override
	public List<WeiBiAccountTrace> findWeiBiAccountTracePageByOpenId(Long weiBiAccountId, int pageIndex, int pageSize) {
		return weiBiAccountTraceRepository.findWeiBiAccountTracePageByOpenId(weiBiAccountId, pageIndex, pageSize);
	}

	@Override
	public WeiBiAccountTrace findByWeiBiAccountIdAndChangeTypeAndChangeDate(Long weiBiAccountId, ChangeType changeType,
			Integer changeDate) {
		return weiBiAccountTraceRepository.findByWeiBiAccountIdAndChangeTypeAndChangeDate(weiBiAccountId, changeType,
				changeDate);
	}

	@Override
	public Page<WeiBiAccountTrace> findWeiBiAccountTracePage(String openId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime) {
		Member member = memberRepository.findMemberByOpenId(openId);
		WeiBiAccount weiBiAccount = weiBiAccountRepository.findWeiBiAccountByCardNoAndOpenId(member.getCardNo(),
				openId);
		if (null != weiBiAccount) {
			return weiBiAccountTraceRepository.findWeiBiAccountTracePage(weiBiAccount.getId(), pageNo, pageSize,
					searchParams, sortParam, sortType, startTime, endTime);
		} else {
			return new Page<>(1, 1);
		}

	}

	@Override
	public WeiBiAccountTrace findById(Long id) {
		return (WeiBiAccountTrace) weiBiAccountTraceRepository.getById(id);
	}

}
